package controllers;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import models.UserInfo;
import play.db.jpa.GenericModel.JPAQuery;
import play.mvc.Controller;

public class Application extends Controller {

    public static void index() {
    	render();
    }
    
    public static void add( String addName, Integer addAge ) {
    	UserInfo userinfo = new UserInfo() ;
    	userinfo.name = addName;
    	userinfo.age = addAge ;
    	userinfo.save() ;
    	redirect("/");
    }


    public static void update(int updateId, String updateName, Integer updateAge ) {
    	
    	UserInfo userinfo = UserInfo.findById( new Long(updateId)) ;
    	userinfo.name = updateName;
    	userinfo.age = updateAge ;
    	userinfo.save();
    	System.err.println( "update->id="+updateId+",age->"+updateAge+"name->"+updateName );
    	redirect("/");
    }
    

    public static void del(int delId) {
    	UserInfo.findById( new Long(delId))._delete();
    	redirect("/");
    }
    
    public static void get(int id) {
    	render(UserInfo.findById( new Long(id)));
    }
    
    
    public static void query( int pageNum, int pageSize ) {
    	
//    	System.err.println( pageNum );
    	
    	if ( pageNum == 0 ) pageNum = 1 ;
    	
    	long rowTotal = UserInfo.count() ;
    	int totalPage = PageUtils.getTotalPage( (int)rowTotal, pageSize ) ;
    	List<UserInfo> userInfos = UserInfo.find( "from UserInfo order by id desc", null ).fetch(pageNum, pageSize) ;
    	
    	Page page = new Page() ;
    	page.setList( userInfos );
    	page.setPageNumber( pageNum );
    	page.setPageSize( pageSize );
    	page.setTotalPage( totalPage );
    	page.setTotalRow( (int)rowTotal );
    	
    	renderJSON( page );
    }
}